<?php
/**
 * @Author:      dx
 * @DateTime:  2017-08-09 14:56:20
 * @Description: 套餐卡 微信的支付记录
 */
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
class WeixinRecord extends Eloquent
{
    protected $table = "weixin_record";

    /**
     * @Author:      dx
     * @DateTime:  2017-08-09 14:25:34
     * @Description: 调用 微信 生成订单
     * @params type 支付的类型
     * @params order_id 业务单号
     * @params buss_id 业务单支付号
     * @params amount 支付金额
     * @params user_id 用户id
     * @params shop_id 门店id
     * @params create_by 创建人
     */
    public static function wx_prepareOrder($type,$order_id,$buss_id,$amount,$user_id,$shop_id,$create_by){
        try{
            return  DB::transaction(function() use ($type,$order_id,$buss_id,$amount,$user_id,$shop_id,$create_by) {
                $desc = '';
                $attach = '';
                switch ($type) {
                    case 1:
                        $attach = '充值记录'.$order_id;
                        $desc = '充值';
                        break;
                    case 2:
                        $attach = '订单支付'.$order_id;
                        $desc = '订单支付';
                        break;
                    case 3:
                        $attach = '洗车单支付'.$order_id;
                        $desc = '洗车单支付';
                        break;
                    case 4:
                        $attach = '订单收回'.$order_id;
                        $desc = '订单收回';
                        break;
                    case 5:
                        $attach = '套餐卡订单支付'.$order_id;
                        $desc = '套餐卡订单支付';
                        break;
                }
                $id=Common::createOrderId();
                $wr=new WeixinRecord;
                $wr->id=$id;
                $wr->order_id=$order_id;
                $wr->type=$type;
                $wr->buss_id=$buss_id;
                $wr->amount=$amount;
                $wr->user_id=$user_id;
                $wr->shop_id=$shop_id;
                $wr->create_by=$create_by;
                $wr->save();
//                $args = array(
//                    'id'=>$id,
//                    'amount'=>$amount,
//                    'desc'=>$desc,
//                    'attach'=>$attach,
//                    'user_id'=>$user_id,
//                    'trade_type'=>$trade_type,
//                );
//                $response = WeixinRecord::wx_pay($args);
//                Log::info('service_card_wx_pay',array('function' => 'wx_prepareOrder','response'=> $response));
//                return $response;
                return $id;
            });
        } catch (Exception $e) {
            Log::error("error", array("system" => "wechat2.0" ,"event" => "wx_prepareOrder" ,"exception" => $e));
            return false;
        }
    }

    /**
     * @Author:    dx
     * @DateTime:  2017-08-09 15:19:04
     * @Description: 调起微信支付 废弃
     */
    public static function wx_pay($args){
        $wx_user=Users::find($args['user_id']);
        $id=$args['id'];
        $order_id=$args['order_id'];
        $amount=$args['amount'];
        $trade_type = $args['trade_type'];
        $attach = $args['attach'];
        $desc = $args['desc'];

        $order=ServiceCardOrder::find($order_id);

        $wx = Config::get('wechat.CHEPIN');
        $options = $wx['OPTION'];
        $app = new Application($options);
        $payment = $app->payment;
        $attributes = [
            'trade_type'       => $trade_type, // JSAPI，NATIVE，APP...
            'body'             => $attach.$order_id,
            'detail'           => $desc,
            'out_trade_no'     => 'service_card-'.$id,
            'total_fee'        => 1,//$amount, // 单位：分  测试1分
            'product_id'       => 'service_card_pay',
            'notify_url'       => Config::get('wechat.pay_note_url'), // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => $wx_user->openid, // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
            // ...
        ];
        $weixin_order = new Order($attributes);
        $result = $payment->prepare($weixin_order);
        Log::info('service_card_wx_pay',array('function' => 'wx_js','response'=> $result));
        if ($result->return_code == 'SUCCESS'&&$result->result_code == 'SUCCESS'){
            $prepayId = $result->prepay_id;
            $config = $payment->configForJSSDKPayment($prepayId); // 这个方法是取得js里支付所必须的参数用的
            $wp=WeixinRecord::where("id",$id)->first();
            if(is_null($wp)){
                $response = array('code' => 1, 'msg' => 'fail');
                return $response;
            }
            $wp->order_id=$order_id;
            $wp->wx_pay_id=$result->prepay_id;
            $wp->status=0;
            $wp->amount=$amount;
            $wp->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array('config'=>$config));
        }else{
            $response = array('code' => 1, 'msg' => 'fail');
        }
        return $response;
    }

}